#version 130

in vec2 vTexCoord;

uniform sampler2D CrtState;
uniform float TimeElapsed;

uniform float ExpandFactor;

const float width = 128.0;
const float height = 128.0;

void main(void)
{
   const float du = 1.0 / width;
   const float dy = 1.0 / height;
   const float du2 = 2.0 * du;
   const float dy2 = 2.0 * dy;

   float state[9];
   state[0] = float(texture2D(CrtState, vTexCoord));
   state[1] = float(texture2D(CrtState, vTexCoord + vec2(-du, -dy)));
   state[2] = float(texture2D(CrtState, vTexCoord + vec2( du, -dy)));
   state[3] = float(texture2D(CrtState, vTexCoord + vec2(-du,  dy)));
   state[4] = float(texture2D(CrtState, vTexCoord + vec2( du,  dy)));
   state[5] = float(texture2D(CrtState, vTexCoord + vec2(  0, -dy)));
   state[6] = float(texture2D(CrtState, vTexCoord + vec2(  0,  dy)));
   state[7] = float(texture2D(CrtState, vTexCoord + vec2(-du,   0)));
   state[8] = float(texture2D(CrtState, vTexCoord + vec2( du,   0)));

   float result = float(texture2D(CrtState, vTexCoord));

   float dt = 0.0;
   for(int i=0; i<9; i++)
      dt += state[i];
   dt *= ExpandFactor * TimeElapsed / 9.0f;

   //gl_FragColor = vec4(dt, 0, 0, 0);
   //gl_FragColor = vec4(result, 0, 0, 0);
   gl_FragColor = vec4(result + dt, 0, 0, 0);
}